<template>
  <div class="ToolingType">
    <el-row style="paddingBottom: 0">
      <el-col :span="24">
        <dynForm :info-form="searchForm" :search-tags="searchTags" :select-list="selectList">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="newAdd"
      >{{ $t("add") }}</el-button
      >
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="doModify"
      >{{ $t("modify") }}</el-button
      >
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="doDelete"
      >{{ $t("delete") }}</el-button
      >
    </div>
    <!-- 表单部分 -->
    <el-card shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <toolingTypeForm ref="toolingTypeForm" @initList="initList"/>
  </div>
</template>

<script>
import mySetting from '@/mixins/mySetting'
import { getList, doDelete } from '@/api/Tooling/toolingType'
export default {
  name: 'ToolingType',
  components: {
    toolingTypeForm: () => import('./component/toolingTypeForm')
  },
  mixins: [mySetting],
  data() {
    return {
      searchForm: {},
      searchTags: [
        { label: this.$t('keyword'), prop: 'name', tag: 'DynInput' }
      ],
      selectList: {},
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'toolingType', label: this.$t('toolingType') },
        { prop: 'mode', label: this.$t('mode'), templete: (_scope) => {
          return (
            <span>{_scope.row.mode === 1 ? this.$t('machineMode') : this.$t('inventoryModel')}</span>
          )
        } },
        { prop: 'category', label: this.$t('category'), templete: (_scope) => {
          const categoryMap = Object.freeze({
            1: '硬件(Socket)',
            2: '备件(消耗品Kit)',
            3: 'Dummy(MP)'
          })
          return (
            <span>{categoryMap[_scope.row.category] }</span>
          )
        } },
        { prop: 'lifeTime', label: this.$t('lifeControl'), templete: (_scope) => {
          return (
            <span>{_scope.row.lifeTime ? this.$t('lifeControl') : this.$t('unlifeControl')}</span>
          )
        } },
        { prop: 'description', label: this.$t('description') },
        { prop: 'createTime', label: this.$t('createTime') }
      ],
      tableLoading: true
    }
  },
  created() {
    this.initList()
  },
  methods: {
    async initList() {
      const res = await getList(this.$clearNull({ ...this.searchForm, ...this.listQuery }))
      this.tableData = res.data
      this.total = res.total
      this.tableLoading = false
    },
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    newAdd() {
      this.$refs.toolingTypeForm.handleOpen()
    },
    doModify() {
      this.$refs.toolingTypeForm.handleOpen(this.multipleSelection[0])
    },
    doDelete() {
      this.$confirm(
        `${this.$t('confirmDelete')}${this.multipleSelection.length}${this.$t(
          'recordCount'
        )}?`,
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map(ele => ele.id)
        doDelete({ ids }).then(res => {
          this.$betterTips(res, this.setOnePage, this.initList)
        })
      })
    },
    getList(page) {
      this.listQuery = page
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
::v-deep .el-form {
  padding-top: 10px;
  //     .el-form-item{
  //   margin-bottom: 15px;
  // }
}
</style>
